export class UIPool<T extends mw.UIScript>{
    private creator:()=>T

    private readonly pool:T[]

    constructor() {
        this.pool = []
    }

    setCreator(func:()=>T){
        this.creator = func
    }

    get():T{
        for (let i of this.pool) {
            if(i.uiObject.visibility == mw.SlateVisibility.Hidden){
                i.uiObject.visibility = (mw.SlateVisibility.Visible)
                return i
            }
        }

        let result = this.creator()
        this.pool.push(result)
        return result
    }

    giveBack(item:T){
        item.uiObject.visibility = (mw.SlateVisibility.Hidden)
    }

    resetAll(){
        for (let item of this.pool) {
            this.giveBack(item)
        }
    }

    eachActive(func:(index:number,item:T)=>void){
        for (let i = 0; i < this.pool.length; i++) {
            func(i,this.pool[i])
        }
    }
}

export class UIRowElementPool<T extends mw.Widget>{
    private creator:()=>T

    private readonly pool:T[]

    constructor() {
        this.pool = []
    }

    setCreator(func:()=>T){
        this.creator = func
    }

    get():T{
        for (let i of this.pool) {
            if(i.visibility == mw.SlateVisibility.Hidden){
                i.visibility = (mw.SlateVisibility.Visible)
                return i
            }
        }

        let result = this.creator()
        this.pool.push(result)
        return result
    }

    giveBack(item:T){
        item.visibility = (mw.SlateVisibility.Hidden)
    }

    resetAll(){
        for (let item of this.pool) {
            this.giveBack(item)
        }
    }
}